﻿#include "TableDisplayWindow.h"
#include <vtkTable.h>
#include <vtkDoubleArray.h>
#include "InfomationGraph3DBasical.h"

#include <QDebug>

namespace pst
{
    TableDisplayWindow::TableDisplayWindow(QWidget* parent)
        : QTableWidget(parent)
        , m_info(nullptr)
    {
    }

    void TableDisplayWindow::addDataToWindow(InfomationGraph3DBasical* info)
    {
        //QTableWidget* tableWidget = new QTableWidget(this);
        ////QVBoxLayout* layout = new QVBoxLayout(ui->tabWidget);
        //ui->tabWidget->addTab(tableWidget, "Table" + QString::number(++m_tableWidgetCount));
        //ui->tabWidget->setCurrentWidget(tableWidget);
        m_info = info;

        QStringList headers;
        auto reader = info->m_reader;
        TextFileParser* dataReader = static_cast<TextFileParser*>(reader);
        if (reader == nullptr || dataReader == nullptr)
        {
            qDebug() << ":===========";
            return;
        }
        auto dataTable = dataReader->getOriginalTable();
        auto rowsCount = dataTable->GetNumberOfRows();
        auto colsCount = dataTable->GetNumberOfColumns();

        this->setColumnCount(colsCount);
        this->setRowCount(rowsCount);

        for (int col = 0; col < colsCount; ++col)
        {
            auto colData = vtkDoubleArray::SafeDownCast(dataTable->GetColumn(col));
            headers << colData->GetName();
            if (colData == nullptr)
            {
                qDebug() << "Array is not doubleArray!";
                return;
            }
            for (int row = 0; row < rowsCount; ++row)
            {
                auto value = colData->GetVariantValue(row).ToDouble();
                QTableWidgetItem* item = new QTableWidgetItem(QString::number(value));
                item->setFlags(item->flags() & ~Qt::ItemIsEditable);
                this->setItem(row, col, item);
            }
        }

        // 表头展示方式
        //auto header = tableWidget->horizontalHeader();
        //header->setSectionResizeMode(QHeaderView::ResizeToContents);
        this->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);

        this->setHorizontalHeaderLabels(headers);
    }
    InfomationGraph3DBasical* TableDisplayWindow::getGraphInfo() const
    {
        return m_info;
    }
}
